$(function () {
    var total = 1;//分页总页面数
    var total_count = 1;//分页总记录数
    var currentPage = 1;//当前页
    var idList = [];//批量选择id所存的数组
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var root_url = $("#root_url").val();

    function init() {
        myPagination();
        bindEvent();
    }

    function bindEvent() {
        //报表标签页导航
        $(".right-section ul.nav.tab-list li a").click(function(){
            var href = $(this).attr("href");
            location.href=href;
        });

        $(".inner-section .search-area #search-btn").click(function () {
            render(true, 1, pageSize);
        });

        //enter事件
        $(".inner-section .search-area input").keydown(function (event) {
            event = event ? event : window.event;
            if (event.keyCode == 13) {
                render(true, 1, pageSize);
            }
        });
    }

    /**
     * 案例分页显示方法
     */
    function myPagination() {
        render(true, 1, pageSize);
        //调用公共分页方法
        pagination("#page-selection", {pageSize: pageSize, total: total}, render);

    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo() {
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function (i, field) {
            selectInfo[field.name] = field.value;
        });
        //JSON.stringify(selectInfo);
        return selectInfo;
    }

    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async, pageIndex, pageSize) {
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async: async,
            type: 'post',
            url: 'pagingMember',
            data: selectInfo,//从1开始计数
            dataType: 'json',
            success: function (result) {
                var html = '';
                if (result.errorCode == 0) {
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total: total, total_count: total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html += '<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="" style="width: 150px;">姓名</th>' +
                        '<th class="">相册</th>' +
                        '<th class="" style="width: 160px !important;">基本信息</th>' +
                        //'<th class="">分享数</th>' +
                        //'<th class="">喜欢数</th>' +
                        '<th class="" style="width: 110px !important;">数量</th>' +
                        '<th class="" style="width: 150px !important;">余额</th>' +
                        //'<th class="">累计余额</th>' +
                        '<th class="">找对象</th>' +
                        '<th class="" style="width: 100px !important;">身份/推荐人</th>' +
                        // '<th class="">是否vip</th>' +
                        // '<th class="">是否视频认证</th>' +
                        // '<th class="">审核信息</th>' +
                        // '<th class="" style="width: 100px;">VIP/栏目认证</th>' +
                        // '<th class="" style="width: 100px;">红娘</th>' +
                        // '<th class="" style="width: 100px;">门店</th>' +
                        '<th class="">注册时间</th>' +
                        '<th class="" style="width: 200px !important;">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for (var i = 0; i < myList.length; i++) {
                        var obj = myList[i];
                        var num = (pageIndex - 1) * pageSize + i + 1;
                        var nick_name = obj.nick_name;
                        var identity_id = obj.identity_id;
                        var username = obj.username;
                        var mobile = obj.mobile;
                        var sex = obj.sex;
                        var age = obj.age;
                        var share_num = obj.share_num;
                        var like_num = obj.like_num;
                        var fans_num = obj.fans_num;
                        var id = obj.id;
                        var is_disable = obj.is_disabled;
                        var balance = obj.balance;
                        var total_balance = obj.total_balance;
                        var add_time = obj.add_time;
                        var user_id = obj.user_id;
                        var photo_list = obj.photo_list;
                        var user_img = obj.user_img;
                        var audit_state = obj.audit_state;
                        var audit_msg = obj.audit_msg;
                        var audit_time = obj.audit_time;
                        var is_vip = obj.is_vip;
                        var is_red_mother = obj.is_red_mother;
                        var is_store = obj.is_store;
                        var is_vip_video = obj.is_vip_video;
                        var is_red_mother_video = obj.is_red_mother_video;
                        var is_store_video = obj.is_store_video;
                        var identity_text = obj.identity_text;
                        var member_team_count = obj.member_team_count;
                        var red_mother_team_count = obj.red_mother_team_count;
                        var checked = (idList.indexOf(id) >= 0) ? "checked" : "";//判断当前记录先前有没有被选中
                        var is_disable_text = '';
                        var is_disable_html = '';
                        if (is_disable) {
                            is_disable_text = '<span style="color: orange;">关闭</span>';
                            is_disable_html = '<a href="javascript:;" class="disabledStatus" style="color: green;" data-status="0" data-id = "' + id + '">开启</a>';
                        } else {
                            is_disable_text = '<span style="color: green;">开启</span>';
                            is_disable_html = '<a href="javascript:;" class="disabledStatus" style="color: orange;" data-status="1" data-id = "' + id + '">关闭</a>';
                        }

                        if (audit_msg == null) audit_msg = '--';
                        if (audit_time == null) audit_time = '--';

                        var audit_state_text = '<span style="color: grey;">待审核</span>';
                        if (audit_state == 1) audit_state_text = '<span style="color: green;">通过</span>';
                        if (audit_state == 2) audit_state_text = '<span style="color: red;">驳回</span>';

                        var check_state_html = '';
                        var audit_html = '';
                        if (audit_state == 0) audit_html = '<a href="javascript:;" data-id="' + id + '" class="auditStatus" style="color: #00a0e9">审核</a>';

                        check_state_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">审核状态：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + audit_state_text + '</span></div>';
                        check_state_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">审核说明：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + audit_msg + '</span></div>';
                        check_state_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">审核时间：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + audit_time + '</span></div>';

                        var member_info = '';
                        member_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">城市：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + obj.city + '</span></div>';
                        //member_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">姓名：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + username + '</span></div>';
                        member_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">电话：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + mobile + '</span></div>';
                        member_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">性别：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + sex + '</span></div>';
                        member_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">年龄：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + age + '</span></div>';

                        var member_image = '--'
                        if (user_img != null) {
                            member_image = '<img class="showUserImg" data-imgs="'+photo_list+'" style="width: 70px;height: 70px;object-fit: cover;" src="' + root_url + '/' + user_img + '" alt="">';
                        }

                        var member_count_info = ''
                        member_count_info += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">转发数：</span><span style="color: green;overflow: hidden;">' + share_num + '</span></div>';
                        member_count_info += '<div class="likeNum" data-id="'+id+'" style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">喜欢数：</span><span style="color: green;overflow: hidden;">' + like_num + '</span></div>';
                        member_count_info += '<div class="fansNum" data-id="'+id+'" style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">粉丝数：</span><span style="color: green;overflow: hidden;">' + fans_num + '</span></div>';

                        var member_balance_html = '';
                        member_balance_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">余额：</span><span style="color: #0b0f12;overflow: hidden;">' + balance + '</span></div>';
                        member_balance_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">冻结余额：</span><span style="color: #0b0f12;overflow: hidden;">' + obj.commission + '</span></div>';
                        member_balance_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">累计余额：</span><span style="color: #0b0f12;overflow: hidden;">' + total_balance + '</span></div>';

                        var is_vip_text = '<span style="color: grey">否</span>'
                        if (is_vip) is_vip_text = '<span style="color: green;">是</span>'
                        var is_vip_video_text = '<span style="color: grey">否</span>'
                        if (is_vip_video > 0) is_vip_video_text = '<span class="showVideo" data-path="'+obj.vip_auth_video+'" style="color: green;cursor: pointer">是</span>'
                        var member_vip_auth_html = ''
                        member_vip_auth_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">VIP开通：</span><span style="color: #0b0f12;overflow: hidden;">' + is_vip_text + '</span></div>';
                        member_vip_auth_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">栏目认证：</span><span style="color: #0b0f12;overflow: hidden;">' + is_vip_video_text + '</span></div>';

                        var is_red_mother_text = '<span style="color: grey">否</span>'
                        if (is_red_mother == 1) is_red_mother_text = '<span style="color: green;">是</span>'
                        var is_red_mother_video_text = '<span style="color: grey">否</span>'
                        if (is_red_mother_video > 0) is_red_mother_video_text = '<span class="showVideo" data-path="'+obj.red_mother_auth_video+'" style="color: green;cursor: pointer">是</span>'
                        var member_red_mother_html = ''
                        member_red_mother_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">红娘开通：</span><span style="color: #0b0f12;overflow: hidden;">' + is_red_mother_text + '</span></div>';
                        member_red_mother_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">红娘视频：</span><span style="color: #0b0f12;overflow: hidden;">' + is_red_mother_video_text + '</span></div>';

                        var is_store_text = '<span style="color: grey">否</span>'
                        if (is_store == 1) is_store_text = '<span style="color: green;">是</span>'
                        var is_store_video_text = '<span style="color: grey">否</span>'
                        if (is_store_video > 0) is_store_video_text = '<span class="showVideo" data-path="'+obj.store_auth_video+'" style="color: green;cursor: pointer">是</span>'
                        var member_store_html = ''
                        member_store_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">门店绑定：</span><span style="color: #0b0f12;overflow: hidden;">' + is_store_text + '</span></div>';
                        member_store_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">门店视频：</span><span style="color: #0b0f12;overflow: hidden;">' + is_store_video_text + '</span></div>';

                        var member_team_html = '<a href="javascript:;" style="color: grey;">会员团队</a>'
                        if (member_team_count) {
                            member_team_html = '<a href="memberTeamList?mid=106&pid=' + id + '" data-id="' + id + '" style="color: red;">会员团队</a>';
                        }

                        var red_mother_team_html = '<a href="javascript:;"  style="color: grey;">红娘团队</a>'
                        if (red_mother_team_count) {
                            red_mother_team_html = '<a href="memberTeamList?mid=106&identity=3&pid=' + id + '" data-id="' + id + '" style="color: red;">红娘团队</a>'
                        }

                        var name_text = '';
                        if (username == null) {
                            name_text = nick_name
                        } else {
                            name_text = username + '<br/>' + '<span style="color: #999;" title="'+nick_name+'">'+nick_name+'</span>'
                        }

                        html += '<tr>'
                            + '<td class="th1">' + num + '</td>'
                            + '<td class="" style="display: table-cell;overflow: hidden;white-space: nowrap;text-overflow: ellipsis">' + name_text + '<br/><span style="color: grey">ID:'+id+'</span></td>'
                            + '<td class="">'+member_image+'</td>'
                            + '<td class="" style="text-align: unset;">' +
                            '<div style="" class="info">' +
                            '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            member_info +
                            '</div>' +
                            '</div>' +
                            '</td>'
                            + '<td class="" style="text-align: unset;">' +
                            '<div style="" class="info">' +
                            '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            member_count_info +
                            '</div>' +
                            '</div>' +
                            '</td>'
                            + '<td class="" style="text-align: unset;">' +
                            '<div style="" class="info">' +
                            '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            member_balance_html +
                            '</div>' +
                            '</div>' +
                            '</td>'
                            //+ '<td class="">' + total_balance + '</td>'
                            + '<td class="">' + is_disable_text + '</td>'
                            + '<td class="">' + identity_text + '</td>'
                            // + '<td class="">' + is_vip_text + '</td>'
                            // + '<td class="" style="text-align: unset;">' +
                            // '<div style="" class="info">' +
                            // '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            // member_vip_auth_html +
                            // '</div>' +
                            // '</div>' +
                            // '</td>'
                            //+ '<td class="">' + is_red_mother_video_text + '</td>'
                            // + '<td class="" style="text-align: unset;">' +
                            // '<div style="" class="info">' +
                            // '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            // member_red_mother_html +
                            // '</div>' +
                            // '</div>' +
                            // '</td>'
                            //+ '<td class="">' + is_store_video_text + '</td>'
                            // + '<td class="" style="text-align: unset;">' +
                            // '<div style="" class="info">' +
                            // '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            // member_store_html +
                            // '</div>' +
                            // '</div>' +
                            // '</td>'
                            + '<td class="">' + add_time + '</td>'
                            + '<td style="text-align: center">'
                            + '<div>'
                            + audit_html
                            + is_disable_html
                            + '<a href="detailMember?id=' + user_id + '" data-id="' + id + '" style="color: black" class="edit-order">详情</a>'
                            + member_team_html
                            + red_mother_team_html
                            + '</div>'
                            + '<div style="margin-top: 10px;">'
                            + '<a href="../WithdrawalAccounts/withdrawalAccounts1List?mid=106&user_id=' + user_id + '" data-id="' + id + '">提现账户</a>'
                            + '<a href="../WithdrawalRecords/withdrawal1List?mid=106&user_id=' + user_id + '" data-id="' + id + '">提现申请</a>'
                            + '<a href="../BalanceRecords/balance1List?mid=106&user_id=' + user_id + '" data-id="' + id + '">收益明细</a>'
                            + '</div>'
                            + '</td>'
                            + '</tr>';
                    }

                    if (myList.length == 0) {
                        html += '<tr><td colspan="' + colspan + '"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    } else {
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList, ".inner-section #list-table .select-all", ".inner-section #list-table .select-single");

                        //收藏
                        $(".likeNum").click(showColloectRecord);
                        //收藏
                        $(".fansNum").click(showColloectRecord2);
                        //vcr
                        $(".showVideo").click(vcr);
                        //照片墙
                        $(".showUserImg").click(showUserImg);
                        //审核
                        $("#list-table .auditStatus").click(auditStatus);
                        //删除
                        $("#list-table .deleteMember").click(deleteMember);
                        //开关
                        $("#list-table .disabledStatus").click(disabledStatus);
                    }

                } else {
                    responseTip(result.errorCode, result.errorInfo, 1500);
                }
            },
            error: errorResponse
        });
    }

    // 收藏记录
    function showColloectRecord () {
        var id = $(this).attr("data-id");
        window.location.href = "../CollectRecord/collectList.html?mid=106&identity=0&user_id="+id;
    }
    function showColloectRecord2 () {
        var id = $(this).attr("data-id");
        window.location.href = "../CollectRecord/collectList.html?mid=106&identity=0&is_fans=1&user_id="+id;
    }

    // 审核
    function auditStatus() {
        var id = $(this).attr("data-id");
        layer.confirm('请选择审核状态?', {
            title: '提示',
            btn: ['通过审核', '审核失败'],
            btn1: function (index, layero) {
                $.ajax({
                    type: "post",
                    url: 'auditStatus',
                    data: {
                        "id": id, 'state': 1
                    },
                    dataType: 'json',
                    success: function (json, statusText) {
                        if (json.errorCode == 0) {
                            if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true, currentPage, pageSize);
                        } else {
                            responseTip(json.errorCode, json.errorInfo, 1500);
                        }
                    }
                })
                layer.closeAll();

            },
            btn2: function (index) {
                layer.closeAll();
                layer.prompt({title: '原因', formType: 2}, function (reason, index) {
                    if (reason == '') {
                        return false;
                    }
                    $.ajax({
                        type: "post",
                        url: 'auditStatus',
                        data: {
                            "id": id, 'state': 2, 'reason': reason
                        },
                        dataType: 'json',
                        success: function (json, statusText) {
                            layer.closeAll();
                            if (json.errorCode == 0) {
                                if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                                    currentPage = currentPage - 1;
                                }
                                render(true, currentPage, pageSize);
                            } else {
                                responseTip(json.errorCode, json.errorInfo, 1500);
                            }
                        }
                    })

                });
            },
        })
    }

    // 看视频
    function vcr() {
        var url = $(this).attr('data-path')
        layer.open({
            type: 2,
            title: false,
            area: ['630px', '360px'],
            shade: 0.8,
            closeBtn: 0,
            shadeClose: true,
            content: url
        });
    }

    // 照片墙
    function showUserImg() {
        let imgs = $(this).attr('data-imgs')
        let items = []
        let src = $(this).attr('src');
        if (imgs == '') {
            items.push({src:src})
        } else {
            imgs = imgs.split(',')
            for (const k in imgs) {
                if (typeof imgs[k] != 'string') {
                    continue;
                }
                items.push({src:root_url+'/'+imgs[k]})
            }
        }
        layer.photos({
            photos: {"data": items},
            shade: .5,
            anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
        });
    }

    // 删除
    function deleteMember() {
        var id = $(this).attr("data-id");
        myConfirmModal("确定删除吗？", function () {
            $.ajax({
                url: "deleteMember",
                type: "post",
                data: {"id": id},
                dataType: "json",
                beforeSend: function (xhr) {
                    //显示“加载中。。。”
                    $("#loading").modal('show');
                },
                complete: function () {
                    //隐藏“加载中。。。”
                    $("#loading").modal('hide');
                },
                success: function (json, statusText) {
                    if (json.errorCode == 0) {
                        if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                            currentPage = currentPage - 1;
                        }
                        render(true, currentPage, pageSize);
                    } else {
                        responseTip(json.errorCode, json.errorInfo, 1500);
                    }
                },
                error: errorResponse
            });
        });
    }

    // 开关
    function disabledStatus() {
        var id = $(this).attr('data-id');
        var status = $(this).attr('data-status');
        myConfirmModal("确定执行此操作吗？", function () {
            $.ajax(
                {
                    type: "post",
                    url: "disabledStatus",
                    data: {"id": id, "state": status},
                    dataType: "json",
                    beforeSend: function (xhr) {
                        //显示“加载中。。。”
                        $("#loading").modal('show');
                    },
                    complete: function () {
                        //隐藏“加载中。。。”
                        $("#loading").modal('hide');
                    },
                    success: function (json, statusText) {
                        if (json.errorCode == 0) {
                            if (currentPage != 1 && total_count % pageSize == 1) {//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true, currentPage, pageSize);
                        } else {
                            responseTip(json.errorCode, json.errorInfo, 1500);
                        }
                    },
                    error: errorResponse
                }
            );
        });
    }

    init();
});